# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=tigervnc
pkgver=1.13.1
pkgrel=5
_xorg_version=21.1.7
pkgdesc="High performance, multi-platform VNC client and server"
url="https://tigervnc.org/"
arch="all"
license="GPL-2.0-or-later"
_makedepends_xorg="
	autoconf
	automake
	ffmpeg-dev
	font-util-dev
	libepoxy-dev
	libpciaccess-dev
	libtool
	libx11-dev
	libxau-dev
	libxcb-dev
	libxcvt-dev
	libxdmcp-dev
	libxext-dev
	libxfont2-dev
	libxkbfile-dev
	libxshmfence-dev
	mesa-dev
	nettle-dev
	pixman-dev
	xcb-util-dev
	xcb-util-image-dev
	xcb-util-keysyms-dev
	xcb-util-renderutil-dev
	xcb-util-wm-dev
	xkbcomp-dev
	xorgproto
	xtrans
	"
makedepends="
	$_makedepends_xorg
	cmake
	fltk-dev
	gettext-dev
	gnutls-dev
	libintl
	libjpeg-turbo-dev
	libxrandr-dev
	libxtst-dev
	linux-pam-dev
	pixman-dev
	samurai
	"
subpackages="$pkgname-openrc $pkgname-doc $pkgname-lang $pkgname-client"
source="
	https://github.com/TigerVNC/tigervnc/archive/refs/tags/v$pkgver/tigervnc-$pkgver.tar.gz
	https://www.x.org/releases/individual/xserver/xorg-server-$_xorg_version.tar.xz
	0001-fix-gettext-intl.patch
	vncserver.initd
	vncserver.confd
	"
options="!check" # no clear test runner

prepare() {
	default_prepare

	# prepare xorg-server tree
	mkdir -p "$builddir"/build-xvnc/unix
	cp -r "$builddir"/unix/xserver "$builddir"/unix/*.patch \
		"$builddir"/build-xvnc/unix
	cp -r "$srcdir"/xorg-server-$_xorg_version/* \
		"$builddir"/build-xvnc/unix/xserver
	cd "$builddir"/build-xvnc/unix/xserver
	patch -p1 < ../xserver21.1.1.patch
	autoreconf -fi
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# build folder needs to be top level for configure
	# soup below to find some outputs........
	cmake -B . -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DINSTALL_SYSTEMD_UNITS=OFF \
		$CMAKE_CROSSOPTS
	cmake --build .

	cd "$builddir"/build-xvnc/unix/xserver
	local _fontroot="/usr/share/fonts"
	# from community/xorg-server
	export CFLAGS="$CFLAGS -D_GNU_SOURCE -D__gid_t=gid_t -D__uid_t=uid_t -Wno-error=array-bounds"
	export CFLAGS="${CFLAGS/-fno-plt}"
	export CXXFLAGS="${CXXFLAGS/-fno-plt}"
	export LDFLAGS="$LDFLAGS -Wl,-z,lazy"
	./configure --disable-config-hal \
		--disable-config-udev \
		--disable-dmx \
		--disable-dri \
		--disable-dri2 \
		--disable-dri3 \
		--disable-kdrive \
		--disable-static \
		--disable-xephyr \
		--disable-xinerama \
		--disable-xnest \
		--disable-xorg \
		--disable-xvfb \
		--disable-xwin \
		--enable-glx \
		--with-default-font-path="$_fontroot/misc,$_fontroot/100dpi:unscaled,$_fontroot/75dpi:unscaled,$_fontroot/TTF,$_fontroot/Type1" \
		--with-pic \
		--with-serverconfig-path=/usr/lib/xorg \
		--with-xkb-bin-directory=/usr/bin \
		--with-xkb-output=/var/lib/xkb \
		--with-xkb-path=/usr/share/X11/xkb \
		--without-dtrace

	make TIGERVNC_SRCDIR="$builddir"
}

package() {
	# /usr/bin/vncserver is written in perl and requires xinit
	# /usr/bin/Xvnc needs xkbcomp and xkeyboard-config to work
	depends="perl xinit xkbcomp xkeyboard-config"

	DESTDIR="$pkgdir" cmake --install .
	install -Dm755 build-xvnc/unix/xserver/hw/vnc/Xvnc \
		-t "$pkgdir"/usr/bin
	mv "$pkgdir"/usr/libexec/vncserver "$pkgdir"/usr/bin
	rmdir "$pkgdir"/usr/libexec

	install -Dm755 "$srcdir"/vncserver.initd "$pkgdir"/etc/init.d/vncserver
	install -Dm644 "$srcdir"/vncserver.confd "$pkgdir"/etc/conf.d/vncserver
}

client() {
	pkgdesc="$pkgdesc (client)"
	amove \
		usr/bin/vncviewer \
		usr/share
}

sha512sums="
9190dbcd3b57ba52286c158c0675104d68463d7e3ea8e23493514b64451ddb511f3daf0f177339bc231155daea376d9c8dc58216663e10aa12f67468f4559da5  tigervnc-1.13.1.tar.gz
e2a093381e28da9b2aa700c6609349fa851f4ca8df23c776f30e4e2733e7a6c1b257576b93f4c4e87fb09df901385bf52528982f6e7a6ad469597aeae8640bb5  xorg-server-21.1.7.tar.xz
1b60d758d2ff727c12fc9f754f2632d62b99f83a71e0f3fc8d07b643c85e18171430becdd36eee84223bc59ebcaf743e4451335fd01c2b0fe764df4853afc583  0001-fix-gettext-intl.patch
a81962b8019e59fd667fb344989c5c38725e803bad43b2fdd977c29bd0b7d32dca1797cf0b6af5ba9071b0a1d7c9628a28b3fafd374ef8241e7e2401406401dc  vncserver.initd
257dba90313eacb38b7f5fcec06fa0b280dcb23d92b91f675c954db4c5adf889e402c903148115934a83f3107477a10a73388320de7c11e203247f04764ddd72  vncserver.confd
"
